<?php
namespace app\admin\controller;
use think\Db;
class Post extends Base
{
    public function index(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['post_type'] = ['in',$role];
            }
            if(session ('area')){
                $map['user_area'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['post_content|user_username'] = ['like',"%" . $key . "%"];
            }
            $map['post_state'] = ['neq',3];
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('posts p')
                ->join('u_user u','u.user_id = p.post_user_id','left')
                ->where($map)
                ->count();
            $lists =  Db::name('posts p')
                ->join('u_user u','u.user_id = p.post_user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('post_add_time')
                ->select ();
            if(!empty($lists)){
                foreach ($lists as $key=>$vo){
                    if($vo['post_type'] == 1){
                        $lists[$key]['post_link'] = explode (',',$vo['post_link']);
                    }
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('post/index');
    }



    public function doCheck(){
        extract (input());
        $res = Db::name('posts')->where(['post_id'=>$p_id])->update (['post_state'=>$type,'post_check'=>2,'post_check_time'=>date('Y-m-d H:i:s')]);
        if($res){
            return json(['code'=>200,'msg'=>'审核成功']);
        }else{
            return json(['code'=>100,'msg'=>'审核失败']);
        }
    }


    public function postDel(){
        extract (input());
        $res = Db::name('posts')->where(['post_id'=>$id])->setField ('post_state',3);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    public function report(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($role)&&$role != ""){
                $map['post_type'] = ['in',$role];
            }
            if(session ('area')){
                $map['user_area'] = ['in',session('area')];
            }
            if(isset($key)&&$key!="")
            {
                $map['post_content|user_username'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('report r')
                ->join('posts p','r.report_post_id = p.post_id','left')
                ->join('u_user u','u.user_id = r.report_user_id','left')
                ->where($map)
                ->count();
            $lists = Db::name('report r')
                ->join('posts p','r.report_post_id = p.post_id','left')
                ->join('u_user u','u.user_id = r.report_user_id','left')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('report_add_time')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('post/report');
    }



    public function doReport(){
        extract (input());
        $report = Db::name('report')->where('report_id',$report_id)->find();
        Db::startTrans ();
        try{
            switch ($type){
                case 1://下架
                    Db::name('posts')->where('post_id',$report['report_post_id'])->setField ('post_state',2);
                    break;
                case 2:
                    Db::name('posts')->where('post_id',$report['report_post_id'])->setField ('post_state',3);
                    break;
            }
            Db::name('report')->where('report_id',$report_id)->setField ('report_status',2);
            Db::commit ();
            return json(['code'=>200,'msg'=>'处理成功']);
        }catch (\Exception $e){
            Db::rollback ();
            return json(['code'=>100,'msg'=>'处理失败']);
        }

    }

}